package tree;

import offer.TreeNode;

public class leetCode543_diameterOfBinaryTree {
    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {

        if(root == null) return 0;
        build(root);
        return ans;
    }

    public int build(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = build(root.left);
        int right = build(root.right);
        ans = Math.max(ans,right + left);
        return Math.max(left,right) + 1;

    }

    int ans2 = 0;
    public int diameterOfBinaryTree2(TreeNode root) {
        if(root == null)return 0;
        build2(root);
        return ans2;
    }
    public int build2(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = build2(root.left);
        int right = build2(root.right);
        ans2 = Math.max(ans2,left + right);
        return Math.max(left,right) + 1;

    }
}
